#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    :   fanyi.py    
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA

@Modify Time      @Author          @Version    @Desciption
------------      -------------    --------    -----------
2020/3/12 6:54 PM   Weiping Huang      1.0         None
"""

import urllib2
import ssl
import json

#登录www.tmxmall.com查看翻译平台

def request(url):
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
    headers={'User-Agent':user_agent,}
    #url="https://www.buscdn.life"
    request = urllib2.Request(url,headers=headers)
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    #Log(urllib2.urlopen(request,context=ctx).read())
    return urllib2.urlopen(request,context=ctx).read()

def tmxmall(text):
    url = "http://api.tmxmall.com/v1/http/mttranslate?mt_provider=general&mt_field=ecomerc&text={}&user_name=hwpchn@gmail.com&client_id=6e267725d8643160f2d45c4c7ef4907a&from=en-US&to=-zh-CN&de=trados".format(text)
    url = urllib2.quote(url, safe='/:?=&') #将url空格替换+，并且设置不替换的符号
    data = json.loads(request(str(url))) #转化json
    return (data['mt_set'][0]['tgt'])


if __name__ == "__main__":
    print(tmxmall("i love you"))
